﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using RioTinto.Helpers;

namespace RioTinto.Web
{
    public class NormalBasePage : System.Web.UI.Page
    {
        #region Protected Properties
        /// <summary>
        /// This property will set the text in the footer of the page.
        /// </summary>
        protected string ApplicationStatus
        {
            set
            {
                ((MasterPage)this.Master).ApplicationStatus = value;
            }
        }
        /// <summary>
        /// This property returns the date that the site was most recently deployed.
        /// </summary>
        protected DateTime DeployDate
        {
            get
            {
                DateTime returnValue = DateTime.Now;

                if (System.IO.File.Exists(Request.PhysicalPath))
                {
                    returnValue = System.IO.File.GetCreationTime(Request.PhysicalPath);
                }

                return returnValue;
            }
        }
        /// <summary>
        /// This property returns the date that the site was most recently configured.
        /// </summary>
        protected DateTime ConfigurationDate
        {
            get
            {
                DateTime returnValue = DateTime.Now;

                if (System.IO.File.Exists(Request.PhysicalApplicationPath + @"\Web.Config"))
                {
                    returnValue = System.IO.File.GetLastWriteTime(Request.PhysicalApplicationPath + @"\Web.Config");
                }

                return returnValue;
            }
        }
        /// <summary>
        /// This property returns the current user's Person object from the Security Manager web service.
        /// </summary>
        public SecurityManager.Person CurrentUser
        {
            get
            {
                string userName = string.Empty;
                SecurityManager.Service service = new SecurityManager.Service();
                SecurityManager.Person person = service.getUser(User.Identity.Name);
                return person;
            }
        }
        #endregion

        #region Page Life Cycle

        protected void Page_PreInit(object sender, EventArgs e)
        {
            //First we will get the current users name to be displayed in the header.
            SecurityManager.Person currentUser = CurrentUser;
            ((MasterPage)this.Master).UserName = currentUser.FirstName + " " + currentUser.LastName;

            //Set the application status (in the footer) to "Ready"
            ApplicationStatus = "Ready";

            //Depending on the current environment selected (Debug, Testing, Staging, Production), we will select a specific theme. 
            switch (EnvironmentState.CurrentEnvironment)
            {
                case Helpers.CurrentEnvironment.Development:
                    this.Theme = System.Configuration.ConfigurationManager.AppSettings["Themes.Development"];
                    break;
                case Helpers.CurrentEnvironment.Testing:
                    this.Theme = System.Configuration.ConfigurationManager.AppSettings["Themes.Testing"];
                    break;
                case Helpers.CurrentEnvironment.Staging:
                    this.Theme = System.Configuration.ConfigurationManager.AppSettings["Themes.Staging"];
                    break;
                case Helpers.CurrentEnvironment.Production:
                    this.Theme = System.Configuration.ConfigurationManager.AppSettings["Themes.Production"];
                    break;
                default:
                    this.Theme = System.Configuration.ConfigurationManager.AppSettings["Themes.Production"];
                    break;
            }
        }
        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();
            Server.ClearError();
            ErrorUtility.CurrentException = ex;
            Server.Transfer("~/Pages/Error.aspx");
        }
        #endregion

        #region Client Script Helpers

        /// <summary>
        /// This will register a client script to run when the page starts.
        /// </summary>
        /// <param name="key">Unique identifier for the client script.</param>
        /// <param name="script">The actual client script code.</param>
        protected void RegisterClientScript(string key, string script)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), key, script, true);
        }

        /// <summary>
        /// This will register a client script file to be included with the page.
        /// </summary>
        /// <param name="key">Unique identifier for the file to be included.</param>
        /// <param name="url">URL of the file to be included.</param>
        protected void RegisterClientScriptInclude(string key, string url)
        {
            ScriptManager.RegisterClientScriptInclude(this.Page, this.GetType(), key, url);
        }
        #endregion
    }
}
